package com.zzyl.common.ai;

import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Component
public class QianfanChatClient {

    private final RestTemplate restTemplate;
    private static final String API_URL = "https://qianfan.baidubce.com/v2/chat/completions";
    private static final String AUTH_TOKEN = "Bearer bce-v3/ALTAK-O6wzc4aOObW8RoRO091vW/e2cbd82af14d0527aa6d19b046ac7e05da0dd743";

    public QianfanChatClient( RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }
    public String sendChatRequest( String  prompt) {
        try {
            // 创建请求体
            Map<String, Object> requestBody = new HashMap<>();
            requestBody.put("model", "ernie-3.5-8k");

            List<Map<String, String>> messages = new ArrayList<>();
            messages.add(createMessage("user", prompt));

            requestBody.put("messages", messages);

            // 设置请求头
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            headers.set("Authorization", AUTH_TOKEN);

            // 创建请求实体
            HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(requestBody, headers);

            // 发送POST请求
            ResponseEntity<Map> response = restTemplate.exchange(
                    API_URL,
                    HttpMethod.POST,
                    requestEntity,
                    Map.class
            );

            // 解析响应并提取 content
            if (response.getStatusCode() == HttpStatus.OK && response.getBody() != null) {
                return extractContentFromMap(response.getBody());
            } else {
                return "请求失败，状态码: " + response.getStatusCode();
            }

        } catch (Exception e) {
            return "请求异常: " + e.getMessage();
        }
    }

    @SuppressWarnings("unchecked")
    private String extractContentFromMap(Map<String, Object> responseBody) {
        try {
            List<Map<String, Object>> choices = (List<Map<String, Object>>) responseBody.get("choices");
            if (choices != null && !choices.isEmpty()) {
                Map<String, Object> firstChoice = choices.get(0);
                Map<String, Object> message = (Map<String, Object>) firstChoice.get("message");

                if (message != null) {
                    return (String) message.get("content");
                }
            }
            return "无法从响应中提取 content 字段";

        } catch (Exception e) {
            return "解析响应失败: " + e.getMessage();
        }
    }

    private Map<String, String> createMessage(String role, String content) {
        Map<String, String> message = new HashMap<>();
        message.put("role", role);
        message.put("content", content);
        return message;
    }

    public static void main(String[] args) {
        QianfanChatClient client = new QianfanChatClient( new RestTemplate());
        String content = client.sendChatRequest( "请给我一个10个字以上的句子");
        System.out.println("AI回复内容: " + content);
    }
}